<template>
  <div class="add-product-container">
    <a-steps :current="current">
      <a-step v-for="item in steps" :key="item.title" :title="item.title" />
    </a-steps>
    <div class="steps-content">
     <!-- form表单 -->
      <BasicDetail v-if="current === 0" @next="next" :form="form"></BasicDetail>
      <SellDetail v-else-if="current===1"
      @back="prev"
      @done="next"
      @checkboxChange="checkboxChange"
      :form="form"></SellDetail>
    </div>
  </div>
</template>
<script>
import api from '@/api/tableData';
import SellDetail from '../../components/SellDetail.vue';
import BasicDetail from '../../components/BasicDetail.vue';

export default {
  components: {
    SellDetail,
    BasicDetail,
  },
  created() {
    // 进入此组件后 判断是新增还是编辑商品  根据路由中有没有id值来判断
    // console.log(this.$route);
    const { id } = this.$route.params;
    if (id) {
      // 有值说明是编辑商品 需要获取商品的form 回填给表单
      api.detail(id).then((res) => {
        console.log(res);
        this.form = res.data;
      });
    }
  },

  data() {
    return {
      current: 0,
      steps: [
        {
          title: '填写商品基本信息',
          content: 'First-content',
        },
        {
          title: '填写商品销售信息',
          content: 'Second-content',
        },
      ],
      form: {
        title: '',
        desc: '',
        category: [],
        c_item: [],
        tags: [],
        price: 0,
        price_off: 0,
        unit: '',
        status: false,
        images: [],
        inventory: 0,
      },

    };
  },
  methods: {
    next(form) {
      this.form = {
        ...this.form,
        form,
      };
      if (this.current === 1) {
      // 判断是新增还是修改原有的
        if (this.$route.params.id) {
          api.edit(this.form).then((res) => {
            console.log(res);
            this.$message.success('修改成功');
            this.$router.push({
              name: 'ProductList',
            });
          });
        } else {
          api.add(this.form).then((res) => {
            console.log(res);
            this.$message.success('新增成功');
            this.$router.push({
              name: 'ProductList',
            });
          });
        }
      } else {
        this.current += 1;
      }
    },
    prev() {
      this.current -= 1;
    },
    checkboxChange(val) {
      // console.log(val);
      this.form.status = Number(val);
      console.log(this.form.status);
    },
  },
};
</script>
<style scoped>
.steps-content {
  margin-top: 16px;
  border: 1px dashed #e9e9e9;
  border-radius: 6px;
  background-color: #fafafa;
  min-height: 200px;
  text-align: center;

}
.ant-steps{
  width: 70%;
  margin: 0 auto;
}

.steps-action {
  margin-top: 24px;
}

</style>
